package com.hq;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class NioClient {
    public static void main(String[] args) throws IOException {
        SocketChannel open = SocketChannel.open();
        open.configureBlocking(false);
        boolean connect = open.connect(new InetSocketAddress("127.0.0.1", 6666));
        // 没有这个判断循环连接 会抛出异常NotYetConnectedException
        if(!connect){
            while (!open.finishConnect()){
                System.out.println("客户端连接未成功");
            }
        }
        String str = "发送测试数据";
        ByteBuffer wrap = ByteBuffer.wrap(str.getBytes());
        open.write(wrap);
        System.in.read();
    }
}
